<?php
/**
 * CONTENIDO extension pack (mpContenidoX), consists of multiple classes to
 * provide more features for Plugins/Modules.
 *
 * @package     mpContenidoX
 * @subpackage  Form
 * @author      Murat Purc <murat@purc.de>
 * @copyright   Copyright (c) 2008-2012 Murat Purc (http://www.purc.de)
 * @license     http://www.gnu.org/licenses/gpl-2.0.html - GNU General Public License, version 2
 * @version     $Id: Element.class.php 21 2012-07-16 21:33:14Z purcaholic $
 */


defined('CON_FRAMEWORK') or die('Illegal call');


/**
 * Form element class
 *
 * @package     mpContenidoX
 * @subpackage  Form
 * @author      Murat Purc <murat@purc.de>
 */
class mpForm_Element
{

    protected $_attributes = array();

    protected $_properties = array();

    protected $_value = null;


    public function __construct(array $element = array())
    {
        if (isset($element['#type'])) {
            // not every element has type attribute
            $this->setProperty('type', $element['#type']);
            unset($element['#type']);
        } elseif (isset($element['type'])) {
            // for elements having type attribute
            $this->setAttribute('type', $element['type']);
            unset($element['type']);
        }

        foreach ($element as $k => $v) {
            if (substr($k, 0, 1) == '#') {
                $k = substr($k, 1);
                $this->setProperty($k, $v);
                if ($k == 'value') {
                    $this->setValue($v);
                }
            } else {
                $this->setAttribute($k, $v);
                if ($k == 'value') {
                    $this->setValue($v);
                }
            }
        }
    }


    public function setAttribute($name, $value)
    {
        $this->_attributes[$name] = $value;
        if ($name == 'value' && $this->_takeoverValues()) {
            if ($this->_value !== $value) {
                $this->_value = $value;
            }
        }
    }


    public function unsetAttribute($name)
    {
        if (isset($this->_attributes[$name])) {
            unset($this->_attributes[$name]);
            if ($name == 'value' && $this->_takeoverValues()) {
                if ($this->_value == $value) {
                    $this->_value = null;
                }
            }
        }
    }


    public function hasAttribute($name)
    {
        return isset($this->_attributes[$name]);
    }


    public function getAttribute($name, $default = null)
    {
        return isset($this->_attributes[$name]) ? $this->_attributes[$name] : $default;
    }


    public function getAttributes()
    {
        return $this->_attributes;
    }


    public function setProperty($name, $value)
    {
        $this->_properties[$name] = $value;
        if ($name == 'value' && $this->_takeoverValues()) {
            if ($this->_value !== $value) {
                $this->_value = $value;
            }
        }
    }


    public function hasProperty($name)
    {
        return isset($this->_properties[$name]);
    }


    public function getProperty($name, $default = null)
    {
        return isset($this->_properties[$name]) ? $this->_properties[$name] : $default;
    }


    public function getProperties()
    {
        return $this->_properties;
    }


    public function addToClassAttribute($value)
    {
        $this->setAttribute('class', trim($this->getAttribute('class', '') . ' ' . $value));
    }


    public function addToStyleAttribute($value)
    {
        $this->setAttribute('style', trim($this->getAttribute('style', '') . ' ' . $value));
    }


    public function setValue($value)
    {
        $this->_value = $value;
        if ($this->_takeoverValues()) {
            if (!isset($this->_attributes['value']) || $this->_attributes['value'] !== $this->_value) {
                $this->_attributes['value'] = $this->_value;
            }
        }
    }


    public function getValue()
    {
        if ($this->_value) {
            return $this->_value;
        }
    }


    public function toArray()
    {
        $array = $this->getAttributes();
        foreach ($this->getProperties() as $key => $value) {
            $array['#' . $key] = $value;
        }
        return $array;
    }


    protected function _takeoverValues()
    {
        switch ($this->getProperty('type')) {
            case 'text':
            case 'password':
            case 'submit':
            case 'reset':
            case 'image':
            case 'hidden':
            case 'checkbox':
            case 'radio':
                return true;
                break;
        }
        return false;
    }

}